﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using software.Common.MVVM;

namespace project.B12.Model.BaseModel
{
    public class BaseEntity : ObservableObject, IDataErrorInfo
    {
        //[SugarColumn(IsPrimaryKey = true)]
        //public Guid Id { get; set; } = Guid.NewGuid();

        ////public bool IsDeleted { get; set; }
        //public DateTime CreationTime { get; set; } = DateTime.Now;

        //[SugarColumn(IsNullable = true)]
        //public DateTime? DeletionTime { get; set; }

        //[SugarColumn(IsNullable = true)]
        //public DateTime? LastModificationTime { get; set; }

        //public virtual void SoftDelete()
        //{
        //    this.IsDeleted = true;
        //    this.DeletionTime = DateTime.Now;
        //}

        //public void NotifyModified()
        //{
        //    this.LastModificationTime = DateTime.Now;
        //}

        #region DataError

        [SugarColumn(IsIgnore = true)]
        public string Error => null;

        /// <summary>
        /// 是否校验通过
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public bool IsValidated
        {
            get
            {
                return this.dataErrors == null || this.dataErrors.Count == 0;
            }
        }

        /// <summary>
        /// 错误信息字典
        /// </summary>

        private Dictionary<string, string> dataErrors = new Dictionary<string, string>();

        [SugarColumn(IsIgnore = true)]
        public string this[string columnName]
        {
            get
            {
                var vc = new ValidationContext(this);
                vc.MemberName = columnName;
                var res = new List<ValidationResult>();
                Validator.TryValidateProperty(this.GetType().GetProperty(columnName)?.GetValue(this), vc, res);
                if (res.Count > 0)
                {
                    var errorInfo = string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray());
                    AddDic(dataErrors, vc.MemberName, errorInfo);
                    return errorInfo;
                }
                RemoveDic(dataErrors, vc.MemberName);
                return null;
            }
        }

        /// <summary>
        /// 移除错误信息
        /// </summary>
        /// <param name="dataErrors"></param>
        /// <param name="dicKey"></param>
        private void RemoveDic(Dictionary<string, string> dataErrors, string dicKey)
        {
            dataErrors.Remove(dicKey);
        }

        /// <summary>
        /// 添加错误信息
        /// </summary>
        /// <param name="dataErrors"></param>
        /// <param name="dicKey"></param>
        /// <param name="errorInfo"></param>
        private void AddDic(Dictionary<string, string> dataErrors, string dicKey, string errorInfo)
        {
            if (!dataErrors.ContainsKey(dicKey)) dataErrors.Add(dicKey, errorInfo);
        }

        #endregion DataError
    }
}
